import app from '@/app'
import store from '@/app/store'
import { get, update, del } from '@/api/erp/tax'

// 初始化
const taxStore = store()

taxStore.get = () => {
  return new Promise((resolve, jeject) => {
    get().then(res => {
      taxStore.setList(res)
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

taxStore.update = (...arge) => {
  return new Promise((resolve, jeject) => {
    update(...arge).then(res => {
      taxStore.get()
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

taxStore.del = (...arge) => {
  return new Promise((resolve, jeject) => {
    del(...arge).then(res => {
      taxStore.get()
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

taxStore.name = name => {
  return taxStore.getList().find(item => item.name == name)
}

taxStore.id = id => {
  return taxStore.getList().find(item => item.id == id)
}

taxStore.tax = id => {
  const tax = taxStore.id(id)
  return tax ? Number(tax.iva) : 0
}

taxStore.req = id => {
  const tax = taxStore.id(id)
  return tax ? Number(tax.req) : 0
}

taxStore.law = id => {
  const tax = taxStore.id(id)
  return tax ? Number(tax.law) : 0
}

taxStore.getName = id => {
  const tax = taxStore.id(id)
  return tax ? tax.name : ''
}

taxStore.default = () => {
  return taxStore.getList().length ? taxStore.getList()[0].id : 0
}
export default taxStore